
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}

function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,

        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}

layer.ready(function () {
    //获取当前用户的部门及人员结构树
    let treeList = getCurrentDepartmentUser();

    if(treeList && treeList.list){
        let userTree = getAllJson(treeList.list);
        getxmselect("#creatorNo","creatorNo", userTree)
    }else {
        getxmselect("#creatorNo","creatorNo", [{name: user.name, value: user.code}])
    }

});

layui.use(['dropdown'], function () {
    let dropdown = layui.dropdown;
    dropdown.onFilter("transferStatistics", function (event) {
        switch (event) {
            case "statisticsTransferAndContractInfo":
                parent.intab("views/jsp/WarehouseManagement/tpl/statisticsTransferAndContractInfo.jsp","外销调拨明细统计")
                break
            case "transferInStockInfoStatistics":
                parent.intab("views/jsp/WarehouseManagement/tpl/transferInStockInfoStatistics.jsp","调拨仓库入库详情")
                break
        }
    });
})

form.render();//表单刷新
table.render({
    elem: '#transfersselect',
    url: 'transfer/listByPage',
    toolbar: '#transferstoolbar',
    cols: [[
        {checkbox: true, fixed: true}
        ,{field:'typeStr', title:'调拨类型'}
        ,{field:'billNo', title:'单号',sort:true,event:'emptyorderon'}
        ,{field:'statusStr', title:'单据类型',sort:true}
        ,{field:'warehouseName', title:'仓库名称'}
        ,{field:'creator', title:'创建人'}
        ,{field:'createDate', title:'创建时间'}
        ,{field:'time', title:'调拨日期',sort:true}
        ,{field:'memo', title:'备注',sort:true}
        ,{fixed: 'right', width:240, toolbar: '#transferseditor', title:'操作'}
    ]],
    id: 'transferstableid',
    height: $(document).height() - $('#transfersselect').offset().top - 50,
    page: true,
    parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    },
});
table.on('tool(transferstable)', function(obj){
    let data = obj.data;
    let id = data.id;
    if(obj.event === 'update') {
        if(data.typeStr === '合同置空'){
            parent.intab("views/jsp/WarehouseManagement/tpl/transferUpdateEmptyContract.jsp?id="+obj.data.id,"修改置空单据")
        }else if(data.typeStr === '合同调拨'){
            parent.intab("views/jsp/WarehouseManagement/tpl/transferUpdateTransfer.jsp?id="+obj.data.id,"修改调拨合同")
        }
    }else if(obj.event === 'delect'){
        layer.confirm("确认删除？",
            confirmInfo(3,6),
            function (index) {
                layer.close(index);
                let dataBases={
                    id:id,
                    status:data.status
                }
                $.ajax({
                    type:"POST",
                    dataType:"json",
                    contentType: 'application/json',
                    data:JSON.stringify(dataBases),
                    url:"transfer/deleteTransfer",
                    success:function(res){
                        if(res.status == 200){
                            layer.msg("删除成功", {icon: 6});
                            table.reload('transferstableid', {
                                page: {
                                    limit:10,
                                    curr: 1 //重新从第 1 页开始
                                }
                                ,where: data.field
                            }, 'data');
                            return false;
                        }else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
    }else if(obj.event === 'submits'){
        layer.confirm('确定提交审批吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 1, "提交审批");
        });
    }else if(obj.event === 'through'){
        layer.confirm('确定审批通过吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 2, "审批通过");
        });
    }else if(obj.event === 'backtothe'){
        layer.confirm('确定审批退回吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 3, "审批退回");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定完成吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 4, "完成");
        });
    }else if(obj.event === 'reduction'){
        layer.confirm('确定还原吗?', confirmInfo(3, 5), function (index) {
            operationData(data, 5, "还原");
        });
    }else if(obj.event === 'emptyorderon'){
        parent.intab("views/jsp/WarehouseManagement/tpl/viewtransferscontract.jsp?id="+obj.data.id,"调拨明细")
    }
});

function operationData(data, operation, msg){
    let fields = {
        id : data.id,
        billNo : data.billNo,
        operation: operation,
        transferType:data.type
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(fields),
        url:"transfer/operationTransfer",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('transferstableid', {
                    page: {
                        limit:10,
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

table.on('toolbar(transferstable)', function(obj){
    switch(obj.event){
        case 'transferscontract':
            parent.intab("views/jsp/WarehouseManagement/tpl/transferscontract.jsp","调拨合同")
            break;
        case 'emptydocument':
            parent.intab("views/jsp/WarehouseManagement/tpl/emptydocument.jsp","置空单据")
            break;
    };
});

form.on('submit(submitform)', function(data){
    table.reload('transferstableid', {
        page: {
            limit:10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$("#statistical").on("click",function () {
    parent.intab("views/jsp/WarehouseManagement/tpl/statistics.jsp","调拨统计")
})

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}